package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    /**
     * 检查组管理新增
     *
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @Override
    public Result add(CheckGroup checkGroup, List<String> checkitemIds) {
        //添加组的信息
        checkGroupDao.add(checkGroup);
        //添加外表信息
        for (String checkitemId : checkitemIds) {
            checkGroupDao.addIds(checkGroup.getId(), checkitemId);
        }

        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS, null);
    }


    /**
     * 检查组分⻚查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public Result findPage(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        Page<CheckGroup> checkGroups = checkGroupDao.findPage(queryPageBean.getQueryString());

        val pageResult = new PageResult();

        pageResult.setTotal(checkGroups.getTotal());
        pageResult.setRows(checkGroups.getResult());

        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, pageResult);
    }

    @Override
    public Result findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);

        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    @Override
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        List<Integer> integerList = checkGroupDao.findCheckItemIdsByCheckGroupId(id);
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, integerList);
    }

    @Override
    public Result edit(CheckGroup checkGroup, List<String> checkitemIds) {
        checkGroupDao.edit(checkGroup);

        checkGroupDao.deleteById(checkGroup.getId());

        for (String checkitemId : checkitemIds) {
            checkGroupDao.addIds(checkGroup.getId(), checkitemId);
        }
        return new Result(true, MessageConst.EDIT_CHECKGROUP_SUCCESS, null);
    }

    @Override
    public Result deleteCheckGroupitemById(Integer id) {
        //校验是否可以删除
        Long count = checkGroupDao.countCheckGroupByCheckItemId(id);
        if(count >0){
            throw new BusinessRuntimeException("当前检查项有数据，不能删除");
        }
        //实际删除操作
        checkGroupDao.deleteGroupById(id);

        return new Result(true, MessageConst.DELETE_CHECKGROUP_FAIL, null);
    }

    @Override
    public Result findCheckGroupIdsBySetmealId(Integer id) {
        List<CheckGroup> checkGroupList =checkGroupDao.findCheckGroupIdsBySetmealId(id);

        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
    }

    @Override
    public Result findAll() {
         List<CheckGroup> checkGroupList=checkGroupDao.findAll();

        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
    }

}
